<template>
  <div class="app-container">

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%">
      <!--查询表单-->
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="objectQuery.transactionId" placeholder="课程名称"/>
        </el-form-item>

        <el-form-item>
          <el-select v-model="objectQuery.state" clearable placeholder="状态">
            <el-option value="true" label="已支付"/>
            <el-option value="false" label="未支付"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="resetData()">清空</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%;margin-left: 1%;margin-right: 1%">
      <!-- 表格 -->
      <el-table :data="list" border stripe style="padding: 30px 0; text-align: center">
        <el-table-column prop="courseName" label="课程名称" width="100" align="center"/>
        <el-table-column prop="userName" label="用户名" width="120" align="center"/>
        <el-table-column prop="fee" label="费用" width="100" align="center"/>

        <el-table-column prop="state" label="支付状态" width="150" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.state === false" type="danger">未支付</el-tag>
            <el-tag v-if="scope.row.state === true" type="success">已支付</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="transactionId" label="交易流水" width="300" align="center"/>
        <el-table-column prop="gmtCreate" label="创建时间" width="250" align="center"/>
        <el-table-column prop="payTime" label="支付时间" width="250" align="center"/>

        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" icon="el-icon-error" @click="remove(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20]"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="fetchData"
      @size-change="changePageSize"/>
  </div>
</template>

<script>
  import orderApi from '@/api/order'

  export default {
    // 页内数据
    data() {
      return {
        list: [],
        page: 1,
        limit: 5,
        total: 0,
        addTitle: "",
        subject: {},
        dialogVisible: false,
        objectQuery:{}
      }
    },

    //初始化
    created() {
      this.fetchData()
    },

    methods: {
      //获取数据
      fetchData(page = 1) {
        this.page = page;
        orderApi.getList(this.page, this.limit, this.objectQuery)
          .then(response => {
            console.log(this.objectQuery)
            this.list = response.data.rows;
            this.total = response.data.total
          })
          .catch(error => {
            console.log(error)
          })
      },

      // 改变每页记录数
      changePageSize(size) {
        this.limit = size;
        this.fetchData()
      },

      // 重置表单
      resetData() {
        this.objectQuery = {};
        this.fetchData()
      },

      // 删除记录
      remove(id) {
        // 询问是否删除
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return orderApi.delete(id)
        }).then(response => {
          // 刷新页面
          this.fetchData();
          // 弹出成功提示
          this.$message({
            message: response.message,
            type: 'success'
          })
        })
      }

    }
  }
</script>
